package com.hsh.starter;


import com.hsh.generate.NewJavaLexer;
import com.hsh.generate.NewJavaParser;
import com.hsh.generate.NewJavaParserBaseListener;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

import java.io.FileInputStream;
import java.io.IOException;

public class ExtractJavaInterfaceTool {
    public static void main(String[] args) throws IOException {
        release();
    }
    private static void release() throws IOException {
        FileInputStream java = new FileInputStream("/opt/oschina/learingcode/antlr/src/main/java/com/hsh/starter/ExtractJavaInterfaceTool.java");

        ANTLRInputStream in = new ANTLRInputStream(java);
        NewJavaLexer lexer = new NewJavaLexer(in);
        CommonTokenStream token = new CommonTokenStream(lexer);
        final NewJavaParser parser = new NewJavaParser(token);
        NewJavaParser.CompilationUnitContext tree = parser.compilationUnit();
        ParseTreeWalker walker = new ParseTreeWalker();

        //listener  解析某个java源文件的所有方法名称
        NewJavaParserBaseListener javaBaseListener = new NewJavaParserBaseListener() {

            @Override
            public void enterClassDeclaration(NewJavaParser.ClassDeclarationContext ctx) {
                System.out.println("class " + ctx.IDENTIFIER() + "{");
            }

            @Override
            public void exitClassDeclaration(NewJavaParser.ClassDeclarationContext ctx) {
                System.out.println("}");
            }

            @Override
            public void enterMethodDeclaration(NewJavaParser.MethodDeclarationContext ctx) {
                TokenStream tokenStream = parser.getTokenStream();
                String type = "void";

                if (ctx.typeTypeOrVoid() != null) {
                    type = tokenStream.getText(ctx.typeTypeOrVoid());
                }
                String args = tokenStream.getText(ctx.formalParameters());
                System.out.println("\t" + type + " " + ctx.IDENTIFIER() + args + ";");
            }

        };

        walker.walk(javaBaseListener, tree);
    }

   /* private static void old() throws IOException {
        FileInputStream java = new FileInputStream("/opt/oschina/learingcode/antlr/src/main/java/com/hsh/starter/ExtractJavaInterfaceTool.java");
        ANTLRInputStream in = new ANTLRInputStream(java);
        JavaLexer lexer = new JavaLexer(in);
        CommonTokenStream token = new CommonTokenStream(lexer);
        final JavaParser parser = new JavaParser(token);
        JavaParser.CompilationUnitContext tree = parser.compilationUnit();

        ParseTreeWalker walker = new ParseTreeWalker();

        //listener  解析某个java源文件的所有方法名称
        JavaBaseListener javaBaseListener = new JavaBaseListener() {

            @Override
            public void enterClassDeclaration(JavaParser.ClassDeclarationContext ctx) {
                System.out.println("class " + ctx.Identifier() + "{");
            }

            @Override
            public void exitClassDeclaration(JavaParser.ClassDeclarationContext ctx) {
                System.out.println("}");
            }

            @Override
            public void enterMethodDeclaration(JavaParser.MethodDeclarationContext ctx) {
                TokenStream tokenStream = parser.getTokenStream();
                String type = "void";

                if (ctx.type() != null) {
                    type = tokenStream.getText(ctx.type());
                }
                String args = tokenStream.getText(ctx.formalParameters());
                System.out.println("\t" + type + " " + ctx.Identifier() + args + ";");
            }
        };

        walker.walk(javaBaseListener, tree);
    }*/
}
